const app = getApp();
import { genTestUserSig } from "../../utils/GenerateTestUserSig.js";
const TIM = require('../../utils/tim-wx.js');
let onSdkReady; //监听tim ready
let onConversationListUpdated; //监听会话列表更新

Page({
  data: {
    user_list: app.globalData.user_list,
    login_user: '', //登录用户
    conversationList: [], //会话列表
  },

  onShow: function () {
    if (app.globalData.tim_ready) {
      this.getContactList(); //拉取会话列表
    }
  },

  onReady: function () {
    let that = this;
    //会话列表更新
    onConversationListUpdated = function (event) {
      console.log('会话列表更新：', event.data); // 包含 Conversation 实例的数组
      that.setData({ conversationList: event.data });
    };
    app.$tim.on(TIM.EVENT.CONVERSATION_LIST_UPDATED, onConversationListUpdated);
  },

  onUnload: function () {
    app.$tim.off(TIM.EVENT.SDK_READY, onSdkReady); //取消监听tim ready
    app.$tim.off(TIM.EVENT.CONVERSATION_LIST_UPDATED, onConversationListUpdated); //取消监听会话列表更新
  },

  //登录
  login: function (e) {
    let that = this;
    let userID = e.currentTarget.dataset.userid;
    let data = genTestUserSig(userID); //生成userSig

    let promise = app.$tim.login({ userID: userID, userSig: data.userSig });
    promise.then(function (imResponse) { //登录成功
      console.log('登录成功：', imResponse.data);
      that.setData({ login_user: userID });
      app.globalData.login_user = userID;
      app.globalData.tim_login_info = imResponse.data;
      //监听 tim ready
      onSdkReady = function (event) {
        console.log('tim ready:', event);
        that.getContactList(); //拉取会话列表
        app.globalData.tim_ready = true; //记录 tim ready
      };
      app.$tim.on(TIM.EVENT.SDK_READY, onSdkReady);
      if (imResponse.data.repeatLogin === true) { //标识账号已登录，本次登录操作为重复登录。v2.5.1 起支持
        console.log('重复登录：', imResponse.data.errorInfo);
        that.getContactList(); //拉取会话列表
      }
    }).catch(function (imError) { //登录失败的相关信息
      console.warn('login error:', imError);
    });
  },

  //登出
  logout: function () {
    let that = this;
    let promise = app.$tim.logout();
    promise.then(function (imResponse) { //登出成功
      console.log('退出登录：', imResponse.data);
      that.setData({ login_user: '' });
      app.globalData.login_user = ''
    }).catch(function (imError) { //登出失败
      console.warn('logout error:', imError);
    });
  },

  //拉取会话列表
  getContactList: function () {
    let that = this;
    let promise = app.$tim.getConversationList(); //拉取会话列表
    promise.then(function (imResponse) {
      console.log('拉取会话列表：', imResponse.data.conversationList);
      that.setData({ conversationList: imResponse.data.conversationList }); // 会话列表，用该列表覆盖原有的会话列表
    }).catch(function (imError) {
      console.warn('getConversationList error:', imError); // 获取会话列表失败的相关信息
    });
  },

  //发起新会话
  playContact: function (e) {
    wx.navigateTo({ url: '../user_list/user_list' });
  },

  //打开会话
  openContact: function (e) {
    let userID = e.currentTarget.dataset.userid;
    let conversationID = 'C2C' + userID;
    wx.navigateTo({ url: '../chat/chat?conversationID=' + conversationID });
  },

})
